#include<iostream>//C
#include<cstdio>
#include<cstring>
using namespace std;
int n,now[6],sum[1001][6],qa,qb,l,qn,qs;
char s[10],a[50];
bool ch1()
{
	for(int i=1;i<=qs;i++)
	{
		for(int j=0;j<=5;j++)
		{
			if(j==5)return 0;
			if(sum[i][j]!=now[j])break;
		}
	}
	return 1;
}
int ch2()
{
	for(int i=1;i<=qs;i++)
	{
		for(int j=0;j<=5;j++)
		{
			if(j==5)return sum[i][5];
			if(sum[i][j]!=now[j])break;
		}
	}
	return -1;
}
int main()
{
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	scanf("%d",&n);
	for(int t=1;t<=n;t++)
	{
		scanf("%s%s",s,a);
		memset(now,0,sizeof(now));
		qa=0,qb=0,l=0,qn=0;
		bool flag=0;
		for(int i=0;i<strlen(a);i++)
		{
			if(a[i]=='.')
			{
				if(qb==1||l==0||now[qn]>255)
				{
					flag=1;
					break;
				}
				else qa++,qn++,l=0;
			}
			else if(a[i]==':')
			{
				if(qb==1||l==0||now[qn]>255)
				{
					flag=1;
					break;
				}
				else qb++,qn++,l=0;
			}
			else
			{
				if(l>0&&now[qn]==0)
				{
					flag=1;
					break;
				}
				else now[qn]=now[qn]*10+a[i]-'0',l++;
			}
		}
		if(flag==1||now[4]>65535||qa!=3||qb!=1)
		{
			printf("ERR\n");
			continue;
		}
		if(s[0]=='S')
		{
			if(ch1()==1)
			{
				printf("OK\n");
				qs++;
				for(int j=0;j<5;j++)sum[qs][j]=now[j];
				sum[qs][5]=t;
			}
			else printf("FAIL\n");
		}
		else
		{
			int ans=ch2();
			if(ch2()==-1)printf("FAIL\n");
			else printf("%d\n",ans);
		}
	}
	return 0;
}

